<?php
namespace app\controller;

use app\model\Devices;
use app\model\DevicesRecord;

class DevicesController
{
    protected $middleware = ['AuthCheck'];

    //获取所有设备
    public function getDevices()
    {
        return rejson(1, ['devices' => Devices::select()]);
    }

    //获取当前设备的零件
    public function getRecord($id)
    {
        $devices = Devices::find($id)->record;
        return rejson(1, ['records' => $devices]);
    }

    //添加一个保养记录
    public function setRecord()
    {
        $id = input('post.id');
        $mark = input('post.mark');
        $username = input('post.username');

        $resRecord = DevicesRecord::create([
            'mark' => $mark,
            'date' => gt(),
            'noteTaker' => $username,
            'devices_id'=>$id
        ]);

        if (!$resRecord) {
            return rejson(0, null, '记录失败', 500);
        }
        return rejson(1, ['resRecord' => $resRecord]);
    }
}
